//
//  NSDate+Extension.swift
//  PMS
//
//  Created by 何海涛 on 15/10/16.
//  Copyright © 2015年 途家. All rights reserved.
//

import Foundation

public class TujiaDateFormatter : NSObject {
    public static var standard: DateFormatter = {
        let formatter = DateFormatter()
        formatter.timeZone = TimeZone(identifier: CommonDefine.kTimeZone)
        formatter.calendar = Calendar.init(identifier: Calendar.Identifier.gregorian)
        return formatter
    }()
}

extension Date {
    public func toString(_ format : String) -> String {
        let formatter = TujiaDateFormatter.standard
        formatter.dateFormat = format
        return formatter.string(from: self)
    }
    
    public func toShortDateString(separaterChar separater : String = ".") -> String {
        return self.toString("yyyy\(separater)MM\(separater)dd")
    }
    
    public var year : Int { return Int(self.toString("yyyy"))! }
    public var month : Int { return Int(self.toString("MM"))! }
    public var day : Int{ return Int(self.toString("dd"))! }
    public var hour : Int{return Int(self.toString("H"))!}
    public var minute : Int{return Int(self.toString("m"))!}
    public var sencond : Int{return Int(self.toString("s"))!}
    public var milliscond : Int{return Int(self.toString("S"))!}
    
    public var isToday: Bool { return toShortDateString() == Date().toShortDateString() }
    
    // 星期天＝1,星期一＝2,星期二＝3 ...
    public var dayOfWeek: Int { get {
        let calendar = TujiaDateFormatter.standard.calendar
        let component = (calendar as NSCalendar?)?.components(.weekday, from: self)
        return component?.weekday ?? 0
        }
    }
    
    public var dayOfWeekStr: String { get {
        let weeks = ["","星期天","星期一","星期二","星期三","星期四","星期五","星期六"];
        return weeks[dayOfWeek]
        }
    }
    
    public var date : Date { return self.toShortDateString().toDate()! as Date }
    
    public func addSeconds(_ seconds : Double) -> Date {
        let interval : TimeInterval = seconds
        return Date(timeInterval: interval, since: self)
    }
    
    public func addMinutes(_ minutes : Double) -> Date {
        return self.addSeconds(minutes * 60)
    }
    
    public func addHours(_ hours : Double) -> Date {
        return self.addMinutes(hours * 60)
    }
    
    public func addDays(_ days : Double) -> Date {
        return self.addHours(days * 24)
    }
    
    public func addMonths(_ months: Int) -> Date {
        let calendar = TujiaDateFormatter.standard.calendar
        var components = DateComponents()
        components.month = months
        return calendar?.date(byAdding: components, to: self) ?? Date()
    }
    
    public func addYears(_ years: Int) -> Date {
        let calendar = TujiaDateFormatter.standard.calendar
        var components = DateComponents()
        components.year = years
        return calendar?.date(byAdding: components, to: self) ?? Date()
    }
    
    public func dayCount() -> Int{
        var endDay = self.addMonths(1)
        endDay = endDay.addDays(Double(-endDay.day))
        return endDay.day
    }
    
    public func daysToEndDate(_ end: Date) -> Int {
        let startDate = self.toString("yyyy-MM-dd").toDate() ?? Date()
        let endDate = end.toString("yyyy-MM-dd").toDate() ?? Date()
        let calendar = TujiaDateFormatter.standard.calendar
        var components = calendar?.dateComponents([.day,.second], from: startDate, to: endDate)
        return components?.day ?? 0
    }
    
    public static var today : Date { return Date().date }
    public var firstDayOfMonth : Date { return self.addDays(Double(-(self.day-1))) }
    public var lastDayOfMonth : Date { return self.addMonths(1).addDays(Double(-self.addMonths(1).day))}
    
    public static func getIntervalDays(_ begin : Date, end : Date) -> [Date] {
        var result = [Date]()
        var i = begin
        while (i <= end) {
            result.append(i)
            i = i.addDays(1)
        }
        return result
    }
    
    public static func getIntervalDaysIgnoreHours(_ begin:Date,end : Date) -> [Date] {
        return  self.getIntervalDays(begin.date, end: end.date)
    }
}

public func NSDateMake(_ year : Int, _ month : Int, _ day : Int) -> Date? {
    return "\(year)/\(month)/\(day)".toDate()
}

extension Date {
    
    public static func toLocalTime(_ date : Date) ->Date{
        let zone = TimeZone.current
        let interVal = zone.secondsFromGMT(for: date)
        let newdate = date.addingTimeInterval(Double(interVal))
        return newdate
    }
    
    public func toLocalDate() -> Date {
        let zone = TimeZone.current
        let interVal = zone.secondsFromGMT(for: self)
        let newdate = self.addingTimeInterval(Double(interVal))
        return newdate
    }
}

extension Int {
    
    public func toDateFomatterString() -> String {
        let  timeDateStr = String(format:"%02lu",self/60) + ":" + String(format: "%02lu",self%60)
        return timeDateStr
    }
}
